package com.example.test.chapter15;

import com.example.test.R;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class RotationThreeDemo extends Activity {
	static final int PICK_REQUEST = 1337;
	Button viewButton = null;
	Uri contact = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupView();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		setupView();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == PICK_REQUEST) {
			if (resultCode == RESULT_OK) {
				contact = data.getData();
				viewButton.setEnabled(true);
			}
		}
	}

	private void setupView() {
		setContentView(R.layout.onsaveinstancestate);
		Button btn = (Button) findViewById(R.id.pickOnSave);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
				startActivityForResult(i, PICK_REQUEST);
			}
		});
		viewButton = (Button) findViewById(R.id.viewOnSave);
		viewButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(Intent.ACTION_VIEW, contact));
			}
		});
		Button btnBack = (Button) findViewById(R.id.backpage);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),
						MainChapter15.class));
				finish();
			}
		});
		viewButton.setEnabled(contact != null);
	}
}
